﻿#include "GameUpgradeScene.h"
#include "GameUtil.h"
#include "StartGameScene.h"
#include "MainGameScene.h"

using namespace cocos2d::extension;

const char * gunPng [] = {
	"gun_01.png",
	"gun_02.png",
	"gun_03.png",
	"gun_04.png",
	"gun_05.png",
	"gun_06.png",
	"gun_07.png",
	"gun_08.png",
	"gun_09.png",
	"gun_10.png",
};

GameUpgradeScene::~GameUpgradeScene(void)
{
}


CCScene* GameUpgradeScene::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        GameUpgradeScene *layer = new GameUpgradeScene();
		if (layer && layer->init(scene)) {
			layer->autorelease();

		} else {
			delete layer;
			layer = NULL;
		}

        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);


    } while (0);

    // return the scene
    return scene;
}

bool GameUpgradeScene::init(CCScene *scene){  
	if (!CCLayer::init()) {  
		return false;  
	}

	// init
	_winSize = CCDirector::sharedDirector()->getWinSize();
	gunLevel = GameUtil::instance()->getSetting()->getGunLevel();
	healthLevel = GameUtil::instance()->getSetting()->getHealthLevel();
	deadlyShootLevel = GameUtil::instance()->getSetting()->getDeadlyShootLevel();

	money_value = GameUtil::instance()->getSetting()->getTotalMoney();
	
	m_pUiLayer = UILayer::create();
    addChild(m_pUiLayer);
        
    m_pWidget = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("MyGame_03_1.json"));
    m_pUiLayer->addWidget(m_pWidget);
		
	// panel
	UIPanel *root = dynamic_cast<UIPanel*>(m_pUiLayer->getWidgetByName("root"));
	root->setPosition(ccp(_winSize.width/2, _winSize.height/2));
	root->setAnchorPoint(ccp(0.5f, 0.5f));

	// buttons
	button_upgrade_01 = dynamic_cast<UIButton*>(m_pUiLayer->getWidgetByName("button_01"));
    button_upgrade_01->addReleaseEvent(this, coco_releaseselector(GameUpgradeScene::btnCallUpgradeHealth));

    button_upgrade_02 = dynamic_cast<UIButton*>(m_pUiLayer->getWidgetByName("button_02"));
    button_upgrade_02->addReleaseEvent(this, coco_releaseselector(GameUpgradeScene::btnCallUpgradeGun));
        
    button_upgrade_03 = dynamic_cast<UIButton*>(m_pUiLayer->getWidgetByName("button_03"));
    button_upgrade_03->addReleaseEvent(this, coco_releaseselector(GameUpgradeScene::btnCallUpgradeSkillDeadlyShoot));
	        
    button_exit = dynamic_cast<UIButton*>(m_pUiLayer->getWidgetByName("exit"));
    button_exit->addReleaseEvent(this, coco_releaseselector(GameUpgradeScene::btnBackCallBack));

	button_next = dynamic_cast<UIButton*>(m_pUiLayer->getWidgetByName("next"));
    button_next->addReleaseEvent(this, coco_releaseselector(GameUpgradeScene::btnNextCallBack));

	updateBtn();

	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("sound/upgrade.mp3", true);
	return true;  
} 


void GameUpgradeScene::btnBackCallBack(CCObject* obj)  
{  
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
	CCDirector::sharedDirector()->replaceScene(StartGameScene::scene());
}  

void GameUpgradeScene::btnNextCallBack(CCObject* obj)  
{  
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
	CCDirector::sharedDirector()->replaceScene(MainGameScene::scene());
}  

void GameUpgradeScene::btnCallUpgradeGun(CCObject* obj)  
{
	if (GameUtil::instance()->getSetting()->upgradeGun()) {
		gunLevel++;
		updateBtn();
	}
}  
void GameUpgradeScene::btnCallUpgradeHealth(CCObject* obj)  
{
	if (GameUtil::instance()->getSetting()->upgradeHealth()) {
		healthLevel++;
		updateBtn();
	}
}  
void GameUpgradeScene::btnCallUpgradeSkillDeadlyShoot(CCObject* obj)  
{
	if (GameUtil::instance()->getSetting()->upgradeDeadlyShoot()) {
		deadlyShootLevel++;
		updateBtn();
	}
}  

void GameUpgradeScene::updateBtn()
{
	money_value = GameUtil::instance()->getSetting()->getTotalMoney();
	money = dynamic_cast<UILabelAtlas*>(m_pUiLayer->getWidgetByName("money_value"));
	char c[50];  
    sprintf(c, "%d", money_value);
	money->setStringValue(c);
	
	level01 = dynamic_cast<UILabelAtlas*>(m_pUiLayer->getWidgetByName("level_01"));
	sprintf(c, "%d", healthLevel);
	level01->setStringValue(c);
		
	level02 = dynamic_cast<UILabelAtlas*>(m_pUiLayer->getWidgetByName("level_02"));
	sprintf(c, "%d", gunLevel);
	level02->setStringValue(c);
		
	level03 = dynamic_cast<UILabelAtlas*>(m_pUiLayer->getWidgetByName("level_03"));
	sprintf(c, "%d", deadlyShootLevel);
	level03->setStringValue(c);

	UIImageView *gun = dynamic_cast<UIImageView*>(m_pUiLayer->getWidgetByName("image_02"));
	gun->setTexture(gunPng[gunLevel]);

	// set the level explainations
	UILabel* c1 = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("current_01"));
	sprintf(c, "current: +%d%% max hp", HEALTH_UPGRADE * healthLevel);
	c1->setText(c);

	UILabel* n1 = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("next_01"));
	if (healthLevel == MAX_HEALTH_LEVEL) {
		sprintf(c, "already max level");
	} else {
		sprintf(c, "next: +%d%% max hp", HEALTH_UPGRADE * (healthLevel + 1));
	}
	n1->setText(c);

	UILabelAtlas* cost1 = dynamic_cast<UILabelAtlas*>(m_pUiLayer->getWidgetByName("cost_01"));
	sprintf(c, "%d", HEALTH_COST * (healthLevel+1));
	cost1->setStringValue(c);


	UILabel* c2 = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("current_02"));
	sprintf(c, "current: +%d attact", GUN_UPGRADE * gunLevel);
	c2->setText(c);

	UILabel* n2 = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("next_02"));
	if (healthLevel == MAX_GUN_LEVEL) {
		sprintf(c, "already max level");
	} else {
		sprintf(c, "next: +%d attact", GUN_UPGRADE * (gunLevel + 1));
	}
	n2->setText(c);

	UILabelAtlas* cost2 = dynamic_cast<UILabelAtlas*>(m_pUiLayer->getWidgetByName("cost_02"));
	sprintf(c, "%d", GUN_COST * (gunLevel+1));
	cost2->setStringValue(c);


	UILabel* c3 = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("current_03"));
	sprintf(c, "current: +%d%%", DEADLY_SHOOT_UPGRADE * deadlyShootLevel);
	c3->setText(c);

	UILabel* n3 = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("next_03"));
	if (healthLevel == MAX_DEADLY_SHOOT_LEVEL) {
		sprintf(c, "already max level");
	} else {
		sprintf(c, "next: +%d%%", DEADLY_SHOOT_UPGRADE * (deadlyShootLevel + 1));
	}
	n3->setText(c);

	UILabelAtlas* cost3 = dynamic_cast<UILabelAtlas*>(m_pUiLayer->getWidgetByName("cost_03"));
	sprintf(c, "%d", DEADLY_SHOOT_COST * (deadlyShootLevel+1));
	cost3->setStringValue(c);
}
